package com.bysj.money_diary;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper{


	private static final String TAG = "DatabaseHelper";
	public static final String USER = "USER";
	public static final String ZHICHU = "ZHICHU";
	public static final String SHOURU = "SHOURU";
	public static final String YUSUAN_MONTH = "YUSUAN_MONTH";
	public static final String ISHIDDEN ="HIDDEN";
	public Context context;
	

	public DatabaseHelper(Context context, String databaseName, Object object, int versionCode) {
		super(context, Data.DATABASE_NAME, null, Data.VERSION_CODE);
		this.context = context;
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		// 创建时的回调
		Log.d(TAG, "创建数据库...");
		saveYuSuan(context,YUSUAN_MONTH,YUSUAN_MONTH,3000);
	    saveYuSuan(context,ISHIDDEN,ISHIDDEN,1);
		//创建字段 
//		String sql = "create table " + Data.TABLE_NAME + "renumber integer primary key,rename text";
//		db.execSQL(sql);
//		db.execSQL("create table SHOURU(sr_id integer primary key,sr_item varchar,sr_year integer,sr_month integer,sr_week integer,sr_day integer,sr_time varchar,sr_count reac,sr_beizhu varchar,sr_pic varchar)");
//		db.execSQL("create table ZHICHU(zc_id integer primary key,zc_item varchar,zc_year integer,zc_month integer,zc_week integer,zc_day integer,zc_time varchar,zc_count reac,zc_beizhu varchar,zc_pic varchar)");
	    db.execSQL("CREATE TABLE IF NOT EXISTS " + 
				ZHICHU + "(" + "ID" + " integer primary key," + 
				Zhichu.ZC_ITEM + " varchar," + 
				Zhichu.ZC_SUBITEM + " varchar," + 
				Zhichu.ZC_YEAR + " Integer," + 
				Zhichu.ZC_MONTH + " Integer," + 
				Zhichu.ZC_WEEK + " Integer," + 
				Zhichu.ZC_DAY + " Integer," + 
				Zhichu.ZC_TIME + " varchar," + 
				Zhichu.ZC_PIC + " varchar," + 
				Zhichu.ZC_COUNT + " REAL," + 
				Zhichu.ZC_BEIZHU + " varchar" + ");");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS " + 
				SHOURU + "(" + "ID" + " integer primary key," + 
				Shouru.SR_ITEM + " varchar," + 
				Shouru.SR_YEAR + " Integer," + 
				Shouru.SR_MONTH + " Integer," + 
				Shouru.SR_WEEK + " Integer," + 
				Shouru.SR_DAY + " Integer," + 
				Shouru.SR_TIME + " varchar," + 
				Shouru.SR_COUNT + " REAL," + 
				Shouru.SR_BEIZHU + " varchar" + ");");
		db.execSQL("CREATE TABLE IF NOT EXISTS " + 
				USER + "(" + "ID" + " integer primary key," + 
				User.U_NAME + " varchar," + 
				User.U_PASSWORD + " varchar," + 
				User.U_BEIZHU + " varchar," + 
				User.U_PHO + " varchar," + 
				User.U_PIC + " BLOB" + ");");
	}
	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// 升级数据库时的回调
		Log.d(TAG, "升级数据库...");
		//添加字段 sql alter table table_name phone integer;
		db.execSQL("DROP TABLE IF EXISTS" + ZHICHU);
		db.execSQL("DROP TABLE IF EXISTS" + SHOURU);
		db.execSQL("DROP TABLE IF EXISTS" + USER);
		onCreate(db);
		
	}
	
	public static void saveYuSuan(Context context, String filename, String name, int num) {
		SharedPreferences preference = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putInt(name, num);
		editor.commit();
	}

	
	public static int readPreferenceFile(Context context,String filename, String name) {
		SharedPreferences preference = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
		int num = preference.getInt(name, 0);
		return num;
	}
}
